package com.psc.view.utils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.psc.R;

/**
 * 继承Dialog弹出透明输入框
 * DialogQuestion dialogCommon = new DialogQuestion(this);
 * dialogCommon.showInputDialog("提问", "请输入您的问题?");
 * dialogCommon.setOnGetInputMsgListener(msg -> {
 * <p>
 * });
 */
public class DialogQuestion extends Dialog {

    public interface OnGetInputMsgListener {
        void onGetInputMsg(String msg);
    }

    private TextView tvTitle;
    private EditText etInput;
    private TextView tvCancel;
    private TextView tvConfirm;

    private OnGetInputMsgListener onGetInputMsgListener;

    public void setOnGetInputMsgListener(OnGetInputMsgListener onGetInputMsgListener) {
        this.onGetInputMsgListener = onGetInputMsgListener;
    }

    public DialogQuestion(@NonNull Context context) {
        super(context, R.style.DialogCommon);
        setContentView(R.layout.dialog_question);
        Window w = getWindow();
        if (w != null) {
            w.setGravity(Gravity.TOP | Gravity.END);//内容居上，偏右。
            WindowManager.LayoutParams lp = w.getAttributes();
            DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            int mHeight = d.heightPixels;
            lp.y = mHeight / 2;//设置Gravity.TOP后可设置内容距离顶部偏移
            //lp.x = 30;// 设置Gravity.END后可设置内容距离右边往左的偏移
            //FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) questionLl.getLayoutParams();
            //params.setMargins(0, mHeight / 2, 0, 0);
            w.setAttributes(lp);
        }
        tvTitle = findViewById(R.id.tv_title);
        etInput = findViewById(R.id.et_input);
        tvCancel = findViewById(R.id.tv_cancel);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvCancel.setOnClickListener(clicks);
        tvConfirm.setOnClickListener(clicks);
    }

    public void showInputDialog(String title, String hint) {
        tvTitle.setText(title);
        etInput.setHint(hint);
        //etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
        //etInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);允许输入数字和小数
        if ("姓名".equals(title) || "描述".equals(title)) {
            etInput.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        //this.onGetInputMsgListener = listener;
        show();
    }

    public View.OnClickListener clicks = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_cancel:
                    dismiss();
                    break;
                case R.id.tv_confirm:
                    if (onGetInputMsgListener != null) {
                        onGetInputMsgListener.onGetInputMsg(etInput.getText().toString().trim());
                    }
                    dismiss();
                    break;
            }
        }
    };


    @Override
    public void dismiss() {
        super.dismiss();
    }


}
